package com.ray.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DeviceBean;
import com.ray.bean.PowerTimeBean;
import com.ray.bean.ReportBean;
import com.ray.bean.TableData;
import com.ray.exception.ServiceException;
import com.ray.mapper.DerviceMapper;
import com.ray.util.ReturnStringUtil;
import com.ray.util.StringUtil;

import net.sf.json.JSONArray;

@Service
public class DeviceService {
	
	@Autowired
	DerviceMapper derviceMapper;

//	public DatatablesViewPageBean getDeviceRealTime(Map map) {
//		int count=0;
//		DatatablesViewPageBean<DeviceBean> dtcpb=new DatatablesViewPageBean<DeviceBean>();
//		count=derviceMapper.getDeviceRealTimeCount(map);
//		dtcpb.setReturnData(derviceMapper.getDeviceRealTime(map));
//		dtcpb.setiTotalDisplayRecords(count);
//		dtcpb.setiTotalRecords(count);
//		return dtcpb;
//	}

    //查询车间电气实时数据
	public DatatablesViewPageBean getPowerDataOnTime(Map map) {
		int count=0;
		DatatablesViewPageBean<DeviceBean> dtcpb=new DatatablesViewPageBean<DeviceBean>();
		count=derviceMapper.getPowerDataOnTimeCount(map);
		dtcpb.setReturnData(derviceMapper.getPowerDataOnTime(map));
		dtcpb.setiTotalDisplayRecords(count);
		dtcpb.setiTotalRecords(count);
		return dtcpb;
	}

	//设置电气action
	public void setPowerAction(Map map) {
		derviceMapper.setPowerAction(map);
	}

	//设置电气车间
	public void setPowerWorkShop(Map map) {
		derviceMapper.setPowerWorkShop(map);
	}

	//设置设备时间
	public void setTime(Map map) throws ServiceException {
		String jsonString=map.get("timeArray").toString();
		JSONArray jsonArray = JSONArray.fromObject(jsonString);//把String转换为 数组 
		List<PowerTimeBean> list= (List<PowerTimeBean>)JSONArray.toCollection(jsonArray, PowerTimeBean.class);
		if(list.size()<=0) {
			throw new ServiceException(ReturnStringUtil.INPUT_TIME_REMIND);
		}
		List<PowerTimeBean> insertList=new ArrayList<PowerTimeBean>();
		List<PowerTimeBean> updateList=new ArrayList<PowerTimeBean>();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String dateString = formatter.format(new Date());
		map.put("updateTime", dateString);
		for(PowerTimeBean deviceTime:list) {
			PowerTimeBean dd=derviceMapper.getPowerTimeBean(deviceTime);
			//如果不存在
			if(null==dd) {
				deviceTime.setId(StringUtil.getUUID());
				insertList.add(deviceTime);
			}else {
				updateList.add(deviceTime);
			}
		}
		if(insertList.size()>0) {
			map.put("insertList", insertList);
			derviceMapper.insertPowerTime(map);
		}
		if(updateList.size()>0) {
			map.put("updateList", updateList);
			derviceMapper.updatePowerTime(map);
		}
		checkTimeUpdatePowerStatus(map.get("powerId").toString());
	}
	
	//检查电气时间更新电气状态
	private void checkTimeUpdatePowerStatus(String powerId) {
        //参数map
		Map paramMap=new HashMap();
		paramMap.put("powerId", powerId);
		Date date=new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("2017-01-01 HH:mm:ss");
		//获取当前时间(日期固定为2017-01-01 比较)
		String nowTime = formatter.format(date);
		System.out.println(nowTime);
		//获取当前星期几
		formatter = new SimpleDateFormat("EEEE");
	    String nowWeek= formatter.format(date);
	    //获取当前日期
	    formatter = new SimpleDateFormat("yyyy-MM-dd");
	    String nowDay=formatter.format(date);
	    
	    paramMap.put("nowTime", nowTime);
	    paramMap.put("nowWeek", nowWeek);
	    paramMap.put("nowDay", nowDay);
	    
	    //获取开始时间为当前时间的数据
	    List<PowerTimeBean> startList=derviceMapper.getBetweenPowerTimeByMap(paramMap);
	    if(startList.size()>0) {
	    	//批量设置电气action为类型
	    	derviceMapper.setListPowerAction(startList);
	    }else {
	    	PowerTimeBean powerTimeBean=new PowerTimeBean();
	    	powerTimeBean.setPowerId(powerId);
	    	startList.add(powerTimeBean);
	    	derviceMapper.setListPowerActionToZero(startList);
	    }
	}

	//获取电气时间，通过电气id
	public List<PowerTimeBean> getPowerTimeByPowerId(Map map) {
		return derviceMapper.getPowerTimeByPowerId(map);
	}

}
